import React, { useState } from "react";
import styled from 'styled-components';
import CommonCard from "@/components/CommonCard/CommonCard";
import { Button, Modal, Table, TableColumnProps, Tag } from '@arco-design/web-react';
import { IconDoubleRight, IconEdit, IconInfo } from "@arco-design/web-react/icon";
import DeviceDetail from "./DeviceDetail/DeviceDetail";
import DeviceJson from "@/configs/device.json";

const Wrapper = styled.div`
    position: relative;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    background: rgb(54, 54, 54);
    padding: 16px;
    overflow: hidden;
`;

const ButtonWrapper = styled.div`
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
`;

const StyledModal = styled(Modal)`
    width: calc(100% - 40px);
    height: calc(100% - 40px);
    border-radius: 5px;
    overflow: hidden;

    div[tabindex="-1"] {
        height: calc(100% - 40px);
    }

    .arco-modal-content {
        height: 100%;
    }
`;

const DeviceManage = () => {
    
    const [ visible, setVisible ] = useState<boolean>(false);
    const [ detail, setDetail ] = useState<IDetail>();

    const goDeviceDetail = (col: IDetail) => {
        setVisible(true);
        setDetail(col);
    };

    const columns: TableColumnProps[] = [
        {
            width: '80px',
            title: '序号',
            dataIndex: 'index',
        },
        {
            title: '设备名称',
            dataIndex: 'name',
        },
        {
            width: '200px',
            title: '设备编号',
            dataIndex: 'number',
        },
        {
            title: '设备地址',
            dataIndex: 'address',
        },
        {
            width: '120px',
            title: '设备IP地址',
            dataIndex: 'ip',
        },
        {
            width: '120px',
            title: '运行状态',
            dataIndex: 'state',
            render: (value, col) => {
                return value === '检修中' ? <Tag  color='#f53f3f' >{ value }</Tag>  : <Tag  color='#7bc616' >{ value }</Tag>
            }
        },
        {
            width: '200px',
            title: '设备创建时间',
            dataIndex: 'createTime',
        },
        {
            width: '350px',
            title: '操作',
            render: (value, col: IDetail) => {
                return <ButtonWrapper>
                    <Button type="primary" style={{ marginRight: '12px' }} status="danger" icon={<IconDoubleRight />}
                        onClick={() => {}}
                    >远程</Button>
                    <Button type="primary" status="warning" icon={<IconInfo />} style={{ marginRight: '12px' }}
                        onClick={() => goDeviceDetail(col)}
                    >详情</Button>
                    <Button icon={<IconEdit />}>编辑</Button>
                </ButtonWrapper>
            },
        }
    ];

    return (
        <Wrapper>
            <CommonCard
                title="设备管理"
                titleRight={
                    <Button type="primary" style={{ margin: '20px 5px 0px 0px' }}>设备录入</Button>
                }
            >
                <Table style={{ width: '100%' }} columns={columns} rowKey={(row) => row.index} data={DeviceJson.data} scroll={{ x: 1200 }}/>;
            </CommonCard>
            <StyledModal
                style={{ backgroundColor: '#1b1b1bef', borderColor: 'black' }}
                visible={visible}
                footer={null}
                onCancel={() => setVisible(false)}
                maskClosable={false}
                // escToExit={false}
            >
                <DeviceDetail
                    detail={detail}
                />
            </StyledModal>
        </Wrapper>
    )
}


export default DeviceManage;